package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Result;
import com.example.springboot.dto.CreativeHomeDTO;
import com.example.springboot.dto.SolutionAllDTO;
import com.example.springboot.entity.CCollection;
import com.example.springboot.entity.Creative;
import com.example.springboot.entity.SCollection;
import com.example.springboot.entity.Solution;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.SCollectionMapper;
import com.example.springboot.service.ISCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.service.ISCommentService;
import com.example.springboot.service.IScoreService;
import com.example.springboot.service.ISolutionService;
import com.example.springboot.utils.Md2Text;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

@Service
public class SCollectionServiceImpl extends ServiceImpl<SCollectionMapper, SCollection> implements ISCollectionService {

    @Resource
    private ISCollectionService collectionService;
    @Resource
    private ISolutionService solutionService;
    @Resource
    private IScoreService scoreService;
    @Resource
    private ISCommentService commentService;

    @Override
    public int getCount(Integer solutionId) {
        QueryWrapper<SCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("solution_id",solutionId);
        return collectionService.list(queryWrapper).size();
    }

    @Override
    public List<SolutionAllDTO> getByUser(Integer userId) {
        QueryWrapper<SCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<SCollection> sCollections = collectionService.list(queryWrapper);
        List<SolutionAllDTO> solutionAllDTOS = new ArrayList<>();
        for (SCollection sCollection : sCollections) {
            Solution solution = solutionService.getById(sCollection.getSolutionId());
            SolutionAllDTO solutionAllDTO = new SolutionAllDTO();
            solutionAllDTO.setId(solution.getId());
            solutionAllDTO.setName(solution.getName());

            String content = Md2Text.convert(solution.getDetail());
            if (content.length() <= 150){
                solutionAllDTO.setDetail(content);
            }
            else {
                solutionAllDTO.setDetail(content.substring(0,150) + "……");
            }
            solutionAllDTO.setScore(scoreService.getScore(solution.getId()));
            solutionAllDTO.setView(solution.getView());
            solutionAllDTO.setCreateTime(solution.getCreateTime().toLocalDate());
            solutionAllDTO.setComments(commentService.getCount(solution.getId()));
            solutionAllDTO.setCollections(collectionService.getCount(solution.getId()));
            solutionAllDTOS.add(solutionAllDTO);

        }
        solutionAllDTOS.sort(Comparator.comparing(SolutionAllDTO::getCreateTime).reversed());
        return solutionAllDTOS;
    }

    @Override
    public boolean judge(Integer userId, Integer solutionId) {
        if (userId == null || solutionId == null)
            throw new ServiceException(Constants.CODE_500,"系统错误");
        QueryWrapper<SCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("solution_id",solutionId);
        SCollection sCollection;
        try {
            sCollection = collectionService.getOne(queryWrapper);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        return sCollection != null;
    }

    @Override
    public boolean deleteBySolution(Integer solutionId) {
        QueryWrapper<SCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("solution_id",solutionId);
        return collectionService.remove(queryWrapper);
    }

    @Override   //新增收藏
    public Result insertCollection(SCollection sCollection) {
        if (judge(sCollection.getUserId(),sCollection.getSolutionId()))
            return Result.error(Constants.CODE_600,"已有收藏关系");
        else {
            Solution solution;
            try {
                solution = solutionService.getById(sCollection.getSolutionId());
            }catch (Exception e) {
                throw new ServiceException(Constants.CODE_500,"系统错误");
            }
            if (solution == null)
                throw new ServiceException(Constants.CODE_600,"未找到方案");
            if (save(sCollection))
                return Result.success("收藏成功");
            else
                return Result.error(Constants.CODE_600,"收藏失败");
        }
    }

    @Override
    public Result removeCollection(SCollection sCollection) {
        if (sCollection.getUserId() == null || sCollection.getSolutionId() == null)
            throw new ServiceException(Constants.CODE_500,"系统错误");
        QueryWrapper<SCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", sCollection.getUserId());
        queryWrapper.eq("solution_id",sCollection.getSolutionId());
        SCollection collection;
        Solution solution;
        try {
            collection = collectionService.getOne(queryWrapper);
            solution = solutionService.getById(sCollection.getSolutionId());
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (collection == null) {
            throw new ServiceException(Constants.CODE_600,"不存在收藏关系");
        }
        if (solution == null) {
            throw new ServiceException(Constants.CODE_600,"未找到方案");
        }
        if (remove(queryWrapper))
            return Result.success(true);
        else
            return Result.error(Constants.CODE_600,"取消收藏失败");
    }
}
